package com.aaa.user.service;

import com.aaa.entity.UmsMemberReceiveAddress;
import com.aaa.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * fileName:UserServiceImpl
 * description:
 * author:lk
 * createTime:2020/1/9 19:38
 * version:1.0.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 根据id获取用户中心的用户信息
     * @param id
     * @return
     */
    @Override
    public List<Map> getUserById(Long id) {
        return userDao.getUserById(id);
    }

    /**
     * 根据用户id查询用户收货地址列表
     */
    @Override
    public List<Map> listAddressByMemberId(long memberId) {
        return userDao.listAddressByMemberId(memberId);
    }

    @Override
    public Map getAddressById(Long id) {
        return userDao.getAddressById(id);
    }

    /**
     * 添加收货地址
     */
    @Override
    public int addAddress(UmsMemberReceiveAddress address) {
        if (address.getDefaultStatus() == 0){
            try {
                Map detail = userDao.getDetailAddress(address.getMemberId());
                detail.put("default_status", 1);
                userDao.updateAddress(detail);
                return userDao.addAddress(address);
            }catch (Exception e) {
                e.printStackTrace();
                return userDao.addAddress(address);
            }
        }else {
            return userDao.addAddress(address);
        }

    }

    /**
     * 查询默认收货地址
     */
    @Override
    public Map getDetailAddress(long memberId) {
        return userDao.getDetailAddress(memberId);
    }

    /**
     * 根据id删除收货地址
     */
    @Override
    public int deleteAddressById(long id) {
        return userDao.deleteAddressById(id);
    }

    /**
     * 修改收货地址
     */
    @Override
    public int updateAddress(Map map) {
        return userDao.updateAddress(map);
    }

    /**
     * 设置默认收货地址
     */
    @Override
    public int setDetailAddress(Map map) {
        if (Integer.parseInt( map.get("default_status").toString()) == 0){
          try {
              Map detail = userDao.getDetailAddress(Integer.parseInt(map.get("member_id").toString()));
              detail.put("default_status", 1);
              userDao.updateAddress(detail);
              return userDao.updateAddress(map);
          }catch (Exception e) {
              e.printStackTrace();
              return userDao.updateAddress(map);
          }
        }else{
            return userDao.updateAddress(map);
        }

    }

    /**
     * 查询用户消费记录
     * @param memberId
     * @return
     */
    @Override
    public List<Map> payHistory(Long memberId) {
        return userDao.payHistory(memberId);
    }
}
